//
//  SLRouteDebugView.m
//  Objective-C
//
//  Created by S_Line on 2018/9/3.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLRouteDebugView.h"

#import "SLDebuger.h"

@interface SLRouteDebugView()

@property (nonatomic, weak) UISegmentedControl *segmentedControl;
@property (nonatomic, weak) UITextField *inputTextField;

@end

@implementation SLRouteDebugView

+ (instancetype)defaultView {
    static SLRouteDebugView *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (sharedInstance == nil) {
            sharedInstance = [[SLRouteDebugView alloc] init];
        }
    });
    
    return sharedInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.tapBlock = ^{
        
    };
    
    self.backgroundColor = [UIColor whiteColor];
    UIColor *themeColor = [UIColor colorWithHexString:@"#222735"];
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat margin = 30;
    CGFloat selfW = screenWidth - margin * 2;
    
    CGFloat subviewLeft = margin;
    CGFloat subviewW = selfW - subviewLeft * 2;
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"native", @"http", @"key"]];
    self.segmentedControl = segmentedControl;
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = themeColor;
    segmentedControl.frame = CGRectMake(subviewLeft, subviewLeft, subviewW, 30);
    [self addSubview:segmentedControl];
    
    CGFloat inputTextFieldY = segmentedControl.frame.origin.y + segmentedControl.frame.size.height + 15;
    UITextField *inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(subviewLeft, inputTextFieldY, subviewW, 30)];
    self.inputTextField = inputTextField;
    inputTextField.borderStyle = UITextBorderStyleLine;
    inputTextField.placeholder = @"请输入路由";
    inputTextField.font = [UIFont systemFontOfSize:13];
    [self addSubview:inputTextField];
    
    CGFloat confirmButtonY = inputTextField.frame.origin.y + inputTextField.frame.size.height + 15;
    UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(subviewLeft, confirmButtonY, subviewW, 44)];
    [confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundImage:[UIImage imageWithColor:themeColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmButton];
    
    CGFloat cancelButtonY = confirmButton.frame.origin.y + confirmButton.frame.size.height + 15;
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(subviewLeft, cancelButtonY, subviewW, 44)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton setBackgroundImage:[UIImage imageWithColor:themeColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelButton];
    
    CGFloat selfH = cancelButton.frame.origin.y + cancelButton.frame.size.height + subviewLeft;
    self.frame = CGRectMake(0, 0, screenWidth - margin * 2, selfH);
    
}

#pragma mark ----- 点击事件
- (void)confirmButtonClick:(UIButton *)sender {
    NSString *k = @"";
    NSString *v = nil;
    if (self.segmentedControl.selectedSegmentIndex == 0) {
        k = [NSString stringWithFormat:@"sloc://%@", self.inputTextField.text];
    } else if (self.segmentedControl.selectedSegmentIndex == 1) {
        k = [NSString stringWithFormat:@"http://%@", self.inputTextField.text];
    } else if (self.segmentedControl.selectedSegmentIndex == 2) {
        k = [NSString stringWithFormat:@"%@", self.inputTextField.text];
    }
    
    [[SLVDNSManager shareInstance] openView:k v:v];
    [[SLDebuger sharedInstance] hideBackView];
}

- (void)cancelButtonClick:(UIButton *)sender {
    [[SLDebuger sharedInstance] hideMenuView:self];
}

@end
